<?php

namespace App\Admin\Controllers;

use App\Libary\Constellation\Constellation;
use App\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use function foo\func;

class MemberController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '会员中心';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());

        $grid->column('id', __('table.users.id'));
        $grid->column('number', __('table.users.number'));
        $grid->column('name', __('table.users.name'));
        $grid->column('email', __('table.users.email'));
        $grid->column('realname', __('table.users.realname'))->display(function ($item){
            return "<p style='color: #F00;'>${item}</p>";
        });
        $grid->column('phone', __('table.users.phone'));
        $grid->column('sex', __('table.users.sex'))->display(function ($value){
            return $value ? '男' : '女';
        });
        $grid->column('marriage', __('table.users.marriage'))->display(function ($value){
            return $value ? "已婚":"未婚";
        });
        $grid->column('birthday', __('table.users.birthday'));
        $grid->column('constellation', __('table.users.constellation'))->display(function ($value){
            return Constellation::get($value);
        });
        #$grid->column('phone_verified_at', __('table.users.phone_verified_at'));
        #$grid->column('email_verified_at', __('table.users.email_verified_at'));
        //$grid->column('password', __('table.users.password'));
        //$grid->column('remember_token', __('table.users.remember_token'));
        $grid->column('created_at', __('Created at'))->date("y-m-d h:i:s");
        $grid->column('updated_at', __('Updated at'))->date("y-m-d h:i:s");

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id', __('table.users.id'));
        $show->field('number', __('table.users.number'));
        $show->field('name', __('table.users.name'));
        $show->field('email', __('table.users.email'));
        $show->field('realname', __('table.users.realname'));
        $show->field('phone', __('table.users.phone'));
        $show->field('sex', __('table.users.sex'));
        $show->field('marriage', __('table.users.marriage'));
        $show->field('birthday', __('table.users.birthday'));
        $show->field('constellation', __('table.users.constellation'));
        $show->field('phone_verified_at', __('table.users.phone_verified_at'));
        $show->field('email_verified_at', __('table.users.email_verified_at'));
        $show->field('password', __('table.users.password'));
        $show->field('remember_token', __('table.users.remember_token'));
        $show->field('created_at', __('created_at'));
        $show->field('updated_at', __('updated_at'));
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User());

        $form->text('number', __('table.users.number'));
        $form->text('name', __('table.users.name'));
        $form->email('email', __('table.users.email'));
        $form->text('realname', __('table.users.realname'));
        $form->mobile('phone', __('table.users.phone'));
        $form->radio('sex', __('table.users.sex'))->options([
            0 => '女',
            1 => '男'
        ])->default(1);
        $form->radio('marriage', __('table.users.marriage'))->options([
            0 => '未婚',
            1 => '已婚'
        ]);
        $form->datetime('birthday', __('table.users.birthday'));
        $form->select('constellation', __('table.users.constellation'))->options(function (){
            return Constellation::view();
        })->width(100);
        $form->password('password', __('table.users.password'));


        $form->saving(function (Form $form) {
            if ($form->password == '') {
                $form->password = $form->model()->password;
            }
        });

        #$form->datetime('phone_verified_at', __('table.users.phone_verified_at'))->default(date('Y-m-d H:i:s'));
        #$form->datetime('email_verified_at', __('table.users.email_verified_at'))->default(date('Y-m-d H:i:s'));
        #$form->text('remember_token', __('table.users.remember_token'));


        return $form;
    }
}
